package com.mmall.seckill.service.impl;

import com.mmall.seckill.common.CodeMsg;
import com.mmall.seckill.common.Result;
import com.mmall.seckill.common.redis.RedisService;
import com.mmall.seckill.common.redis.keyCode.SeckillUserKey;
import com.mmall.seckill.dao.SeckillUserDao;
import com.mmall.seckill.domain.SeckillUser;
import com.mmall.seckill.exception.GlobalException;
import com.mmall.seckill.service.SeckillUserService;
import com.mmall.seckill.util.CookieUtil;
import com.mmall.seckill.util.JsonUtil;
import com.mmall.seckill.util.MD5Util;
import com.mmall.seckill.util.UUIDUtil;
import com.mmall.seckill.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by megan on 2018/6/16.
 */
@Service
public class SeckillUserServiceImpl  implements SeckillUserService {

    @Resource
    private SeckillUserDao seckillUserDao;
    @Resource
    private RedisService redisService;

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @Override
    public SeckillUser getByToken(String token){
        if(StringUtils.isEmpty(token)){
            return null;
        }
        return JsonUtil.string2Obj(redisService.get(SeckillUserKey.token,token),SeckillUser.class);
    }

    /**
     * 对象级缓存,缓存user信息
     * @param id
     * @return
     */
    public SeckillUser getById(Long id){
        //取缓存
        SeckillUser user = JsonUtil.string2Obj(redisService.get(SeckillUserKey.getById,id+""),SeckillUser.class);
        if(user!=null){
           return user;
        }
        // 取数据库
        user = seckillUserDao.getById(id);
        if(user!=null){
            // 写缓存
            redisService.setEx(SeckillUserKey.getById,id+"",JsonUtil.obj2String(user),SeckillUserKey.TOKEN_EX);
        }
        return user;
    }

    public boolean updatePassword(String token ,Long id, String passwordNew){
        //取user
        SeckillUser user = getById(id);
        if(user == null){
            throw new GlobalException(CodeMsg.MOBILE_EMPTY);
        }
        // 更新数据库
        SeckillUser toBeUser = new SeckillUser();
        String dbPassword = MD5Util.formPassToDBPass(passwordNew,user.getSalt());
        toBeUser.setPassword(dbPassword);
        seckillUserDao.update(toBeUser);
        //处理缓存
        redisService.del(SeckillUserKey.getById,id+"");
        user.setPassword(dbPassword);
        redisService.setEx(SeckillUserKey.token,token,JsonUtil.obj2String(user),SeckillUserKey.TOKEN_EX);
        return true;
    }

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public Result login(HttpServletResponse response,LoginVo loginVo){
        //登录
        if(loginVo == null){
            throw new GlobalException(CodeMsg.ERROR);
        }
        //校验手机号
        SeckillUser user = getById(Long.parseLong(loginVo.getMobile()));
        if(user == null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //校验密码
        String formPass = loginVo.getPassword();
        String dbPass = user.getPassword();
        String saltDB = user.getSalt();
        String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
        if(!calcPass.equals(dbPass)) {
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        // 生成一个token,写入浏览器cookie中
        String token = UUIDUtil.uuid();
        redisService.setEx(SeckillUserKey.token,token, JsonUtil.obj2String(user),SeckillUserKey.TOKEN_EX);
        CookieUtil.writrLoginTokenEx(response,token,SeckillUserKey.token.expireSeconds());
        return Result.success(true);
    }
}
